package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ActivityMgr;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.ConfigMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class AcRecharge extends BaseRepair {

    public AcRecharge(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(int activityId){
        List<Map<String, String>> listMap = new ArrayList<>();

        BaseMgr.getMgr(ActivityMgr.class).initActivity(activityId);
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if(activityInfo == null){
            System.err.println("没有找到活动");return;
        }
        if(!activityInfo.getServerIdList().contains(getGameServer().getServerInfo().getServerId())){
            System.err.println(activityId + "不是" + getGameServer().getServerInfo().getServerId()+"的活动");return;
        }
        String dateStart = DateHelper.getCurrentDateTimeString(activityInfo.getBeginShowTime()*1000);
        String dateEnd = DateHelper.getCurrentDateTimeString(activityInfo.getEndShowTime()*1000);
        dateStart = "2021-1-17 10:00:00";
        dateEnd = "2021-1-18 22:00:00";
        listMap = getRechargeCount(activityId,dateStart,dateEnd);
        DataMgr.addData(listMap);
    }

    private List<Map<String, String>> getAcUserCount(String dateStart,String dateEnd) {
        long startTimeStamp = DateHelper.str2timestamp(dateStart + " 00:00:00")/1000;
        long endTimeStamp = DateHelper.str2timestamp(dateEnd + " 00:00:00")/1000;
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select count(distinct(userId)) as userCount from t_u_user WHERE lastLoginTime >= ? AND lastLoginTime <= ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++,startTimeStamp);
            call.setLong(i++,endTimeStamp);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("活跃用户",checkNullToEmpty(rs.getString("userCount")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getRechargeUser(String dateStart,String dateEnd,List<Map<String, String>> idataList) {
        long startTimeStamp = DateHelper.str2timestamp(dateStart + " 00:00:00")/1000;
        long endTimeStamp = DateHelper.str2timestamp(dateEnd + " 00:00:00")/1000;
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select count(distinct(userId)) as ruserCount from t_u_recharge_detail WHERE createTime >= ? AND createTime <= ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            Map<String,String> temp = new ConcurrentHashMap<>();
            while (rs.next()) {
                temp.put(String.valueOf(getGameServer().getServerInfo().getServerId()),checkNullToEmpty(rs.getString("ruserCount")));
            }
            for(Map<String,String> serverData : idataList){
                if(temp.containsKey(serverData.get("serverId"))){
                    serverData.put("充值用户",temp.get(serverData.get("serverId")));
                }else{
                    serverData.put("充值用户","0");
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }

    private List<Map<String, String>> getRechargeCount(int activityId,String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select *,count(1) as num from t_u_recharge_detail WHERE createTime >= ? AND createTime <= ? GROUP BY mallId,userId";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("activityId",String.valueOf(activityId));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("mallId",checkNullToEmpty(rs.getString("mallId")));
                temp.put("amount",checkNullToEmpty(rs.getString("amount")));
                temp.put("num",checkNullToEmpty(rs.getString("num")));
                temp.put("mallName", ConfigMgr.getMallName(Integer.valueOf(temp.get("mallId"))));
                dataList.add(temp);
            }

        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

}
